/**
https://leetcode-cn.com/problems/permutations/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liweiw/
给定一个不含重复数字的数组 nums ，返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
输入：nums = [1,2,3]
输出：[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
*/


class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res=new ArrayList();
        int depth=nums.length;
        boolean[] visited=new boolean[depth];
        List<Integer> path = new ArrayList<>();
        if (depth<=0){
            return res;
        }
        dfs(nums,depth,0,path,visited,res);
        return res;
    }
    public void dfs(int[]nums,int depth,int n,List<Integer> path,boolean[] visited,List<List<Integer>> res){
        if(n==depth){
            res.add(new ArrayList(path));
            return;
        }
        for(int i=0;i<depth;i++){
            if(visited[i]){
                continue;
            }
            visited[i]=true;
            path.add(nums[i]);
            dfs(nums,depth,n+1,path,visited,res);
            visited[i]=false;
            path.remove(path.size()-1);
        }
    }
}